﻿using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
using Cmd;
using GameData;

public class UtilTool  {


    public static void ChangeLevel(string level,bool isflag=true)
    {
        Application.LoadLevel(level);
        if (isflag)
        {
           // TNAutoJoin.instance.firstLevel = level;
        }
    }

    public static string getNumbertoStr(int num)
    {
        string lestr = num.ToString();
        if (lestr.Length == 2)
        {
            lestr = "0" + lestr;
        }
        else if (lestr.Length == 1)
        {
            lestr = "00" + lestr;
        }

        return lestr;
    }


    public static int SortCompareByValue(NumberInfo AF1, NumberInfo AF2)
    {
        int res = 0;
        if (AF1.value > AF2.value)
        {
            res = 1;
        }
        else if (AF1.value < AF2.value)
        {
            res = -1;
        }
        return res;
    }


    public static bool checkNumInDic(List<NumberInfo> dic, NumberInfo num)
    {
        int count = dic.Count;
        NumberInfo temp = null;
        for (int i = 0; i < count; i++)
        {
            temp = dic[i];
            if (temp.value == num.value)
            {
                return true;
            }
        }

        return false;

    }



    /// <summary>

    /// 根据文件尝试返回字符编码

    /// </summary>

    /// <param name="file">文件路径</param>

    /// <param name="defEnc">没有BOM返回的默认编码</param>

    /// <returns>如果文件无法读取，返回null。否则，返回根据BOM判断的编码或者缺省编码（没有BOM）。</returns>

    public static Encoding GetEncoding(string file, Encoding defEnc)

    {

        using (var stream = File.OpenRead(file))

        {

            //判断流可读？

            if (!stream.CanRead)

                return null;

            //字节数组存储BOM

            var bom = new byte[4];

            //实际读入的长度

            int readc;



            readc = stream.Read(bom, 0, 4);



            if (readc >= 2)

            {

                if (readc >= 4)

                {

                    //UTF32，Big-Endian

                    if (CheckBytes(bom, 4, 0x00, 0x00, 0xFE, 0xFF))

                        return new UTF32Encoding(true, true);

                    //UTF32，Little-Endian

                    if (CheckBytes(bom, 4, 0xFF, 0xFE, 0x00, 0x00))

                        return new UTF32Encoding(false, true);

                }

                //UTF8

                if (readc >= 3 && CheckBytes(bom, 3, 0xEF, 0xBB, 0xBF))

                    return new UTF8Encoding(true);



                //UTF16，Big-Endian

                if (CheckBytes(bom, 2, 0xFE, 0xFF))

                    return new UnicodeEncoding(true, true);

                //UTF16，Little-Endian

                if (CheckBytes(bom, 2, 0xFF, 0xFE))

                    return new UnicodeEncoding(false, true);

            }


            stream.Close();
            return defEnc;

        }

    }



    //辅助函数，判断字节中的值

    static bool CheckBytes(byte[] bytes, int count, params int[] values)

    {

        for (int i = 0; i < count; i++)

            if (bytes[i] != values[i])

                return false;

        return true;

    }



}


/// <summary>
/// 身份证阅读类
/// </summary>
class CVRSDK
{
    [DllImport("termb", EntryPoint = "CVR_InitComm", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern int CVR_InitComm(int Port);//声明外部的标准动态库, 跟Win32API是一样的
    [DllImport("termb", EntryPoint = "CVR_Authenticate", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern int CVR_Authenticate();
    [DllImport("termb", EntryPoint = "CVR_Read_Content", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern int CVR_Read_Content(int Active);
    [DllImport("termb", EntryPoint = "CVR_CloseComm", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern int CVR_CloseComm();
    [DllImport("termb", EntryPoint = "GetPeopleName", CharSet = CharSet.Ansi, SetLastError = false)]
    public static extern int GetPeopleName(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetPeopleNation", CharSet = CharSet.Ansi, SetLastError = false)]
    public static extern int GetPeopleNation(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetPeopleBirthday", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetPeopleBirthday(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetPeopleAddress", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetPeopleAddress(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetPeopleIDCode", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetPeopleIDCode(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetDepartment", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetDepartment(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetStartDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetStartDate(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetEndDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetEndDate(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "GetPeopleSex", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetPeopleSex(ref byte strTmp, ref int strLen);
    [DllImport("termb", EntryPoint = "CVR_GetSAMID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int CVR_GetSAMID(ref byte strTmp);
    [DllImport("termb", EntryPoint = "GetManuID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetManuID(ref byte strTmp);


}